package com.test.liuhb.test.Util;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;

import com.android.print.sdk.PrinterConstants;
import com.android.print.sdk.PrinterInstance;
import com.android.print.sdk.bluetooth.BluetoothPort;

/**
 * Created by xu.ch on 2016/7/6.
 */
public class BluetoothOperation implements IPrinterOpertion {

    private BluetoothAdapter mAdapter;
    private Context mContext;
    private Handler mHandler;
    private PrinterInstance mPrinter;
    private IntentFilter filter;
    private String address;
    private BluetoothDevice mDevice;
    public static boolean hasRegDisconnectReceiver;

    public BluetoothOperation(Context context, Handler handler) {
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mContext = context;
        mHandler = handler;


        filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        mContext.registerReceiver(myReceiver, filter);
        hasRegDisconnectReceiver = true;

    }


    public BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice device = intent
                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {

                if (null != device && null != mPrinter
                        && mPrinter.isConnected() && device.equals(mDevice)) {
                    close();
                }

                mHandler.obtainMessage(PrinterConstants.Connect.CLOSED).sendToTarget();
            }
        }
    };


    @Override
    public void open(Intent data) {
        address = data.getExtras().getString("device_address");
        mPrinter = new BluetoothPort().btConnnect(mContext, address, mAdapter, mHandler);
    }

    @Override
    public void close() {
        if (null != mPrinter) {
            mPrinter.closeConnection();
            mPrinter = null;
        }
        if (hasRegDisconnectReceiver) {
            mContext.unregisterReceiver(myReceiver);
            hasRegDisconnectReceiver = false;
        }

    }

    @Override
    public void chooseDevice() {
        Intent intent = new Intent(mContext, PrinterConnectActivity.class);
        ((Activity) mContext).startActivityForResult(intent,
                11);

    }

    @Override
    public PrinterInstance getPrinter() {
        if (null != mPrinter && mPrinter.isConnected()) {
            if (!hasRegDisconnectReceiver) {
                mContext.registerReceiver(myReceiver, filter);
                hasRegDisconnectReceiver = true;
            }
        }
        return mPrinter;
    }


    @Override
    public void btAutoConn(Context context, Handler mHandler) {
        mPrinter = new BluetoothPort().btAutoConn(context, mAdapter, mHandler);
        if (null == mPrinter) {
            mHandler.obtainMessage(PrinterConstants.Connect.NODEVICE).sendToTarget();
        }
    }
}
